﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FreezableModelBase.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the FreezableModelBase type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.ComponentModel.Modelling
{
    using System;

    public class FreezableModelBase : ModelBase, IFreezable
    {
        #region Fields

        private bool _isFrozen;

        #endregion

        public void Freeze()
        {
            if (!IsFrozen)
            {
                IsFrozen = true;
            }
        }

        public bool IsFrozen
        {
            get
            {
                return this._isFrozen;
            }
            private set
            {
                this.SetProperty(ref this._isFrozen, value, "IsFrozen");
            }
        }

        protected override bool SetProperty<T>(ref T field, T value, string propertyName, PropertyChangedCallback<T> propertyChangeCallback = null)
        {
            if (IsFrozen)
            {
                throw new InvalidOperationException("Object is frozen");
            }

            return base.SetProperty<T>(ref field, value, propertyName, propertyChangeCallback);
        }
    }
}
